package com.summer_administration.controller;

import com.summer_administration.mapper.GroupeMapper;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Publicity;
import com.summer_administration.pojo.Result;
import com.summer_administration.service.PublicityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/publicity")
@Validated
public class PublicityController {
    @Autowired
    private PublicityService publicityService;

    @Autowired
    private GroupeMapper groupeMapper;

    @GetMapping
    public Result<PageBean<Publicity>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        PageBean<Publicity> pb = publicityService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Publicity publicity) {
        publicityService.update(publicity);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        publicityService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody @Validated Publicity publicity) {
        // 新增一个根剧小组名字查询小组id
        String groupeName = publicity.getGroupName();
        String groupId = groupeMapper.findGroupIdByGroupName(groupeName);
        publicity.setGroupId(groupId);
        publicityService.add(publicity);
        return Result.success();
    }
}